/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaclientcp3110.DAO.SocketDAO;

import common.DAO.ProductDAO;
import common.DAO.CategoryDAO;
import common.DAO.ComponentDAO;
import common.DAO.Factory.DAOFactory;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import javaclientcp3110.View.LoadingView;
import javaclientcp3110.View.MainView;
import javax.swing.SwingWorker;

/** This is the data access object factory for mysql.
 * This class holds the driver, url, username and password for the database.
 * It also create connection function and getters for the DAOs
 * @author Richard Irons
 */
public class SocketDAOFactory extends DAOFactory {
    private final static int PORT = 6881;
    private static final String HOSTNAME = "localhost"; //137.219.47.188
    private static LoadingView loadingView;

    public String sendXML(String xml) {        
        final String xmlToSend = xml;

        if (loadingView == null) {
            loadingView = new LoadingView();
        }
        loadingView.startLoading();
        MainView.getInstance().setVisible(false);
        SwingWorker worker = new SwingWorker<String, Void>() {
            @Override
            protected String doInBackground() throws Exception {
                try {
                    Socket socket = new Socket(HOSTNAME, PORT);
                    BufferedReader in = new BufferedReader(new InputStreamReader(
                            socket.getInputStream()));
                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                            socket.getOutputStream()));
                    out.write(xmlToSend+"\n");
                    out.flush();

                    String responseXML = in.readLine();

                    System.out.println(responseXML);
                    socket.close();
                    return responseXML;
                } catch (Exception e) {
                    MainView.getInstance().errorMessage("Could not connect to server");
                    System.exit(-1);
                }
                return "";
            }
            @Override
            protected void done() {
                loadingView.finishLoading();
                if (LoadingView.isFinished()) {
                    loadingView.setVisible(false);
                    MainView.getInstance().setVisible(true);
                }
            }
        };
        loadingView.setVisible(true);
        worker.execute();
        try {
           xml = (String) worker.get();
           return xml;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * Getter to create a CategoryDAO
     * @return
     * new MySQLComponentDAO
     */
    public CategoryDAO getCategoryDAO(){
        return new SocketCategoryDAO();
    }

    public ComponentDAO getComponentDAO() {
        return new SocketComponentDAO();
    }

    public ProductDAO getProductDAO() {
        return new SocketProductDAO();
    }
}
